package com.geeklan.smart.ui.settings;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.AppProblem;
import com.geeklan.smart.data.model.VersionResponse;
import com.geeklan.smart.data.repository.msg.CommonRepository;
import com.geeklan.smart.databinding.ActivityCommonProblemBinding;
import com.geeklan.smart.ui.settings.adapter.FunctionAdapter;
import com.geeklan.smart.ui.settings.adapter.ProblemAdapter;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModel;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModelFactory;
import com.geeklan.smart.widget.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class CommonProblemActivity extends BaseActivity<ActivityCommonProblemBinding> {
    private ProblemAdapter problemAdapter;
    private CommonViewModel mViewModel;
    private List<AppProblem> mData = new ArrayList<>();
    private int limit = 20;//每页多少条
    private int page = 1;//页码
    private int pageSize = 1;
    private int count = 0;//页码
    private int refreshType = 0;

    @Override
    protected ActivityCommonProblemBinding initBinding() {
        return ActivityCommonProblemBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this, new CommonViewModelFactory(new CommonRepository())).get(CommonViewModel.class);

        mViewModel.getProblemListLiveData().observe(this, response -> {
            if (response.getData() != null && !response.getData().isEmpty()) {

                if (refreshType == 0) mData.clear();

                this.count = response.getCount();
                pageSize = count % limit > 0 ? (count / limit) + 1 : count / limit;
                mData.addAll(response.getData());
                problemAdapter.notifyDataSetChanged();
                if (refreshType == 0) mBinding.rvProblem.scrollToPosition(0);
            }

            if(mData.isEmpty()){
                mBinding.llNotData.setVisibility(View.VISIBLE);
                mBinding.rvProblem.setVisibility(View.GONE);
            }else {
                mBinding.llNotData.setVisibility(View.GONE);
                mBinding.rvProblem.setVisibility(View.VISIBLE);
            }

            mBinding.smartRefreshLayout.finishRefresh();
            dismiss();
        });

        mViewModel.getProblemErrorLiveData().observe(this, s -> {
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            mBinding.smartRefreshLayout.finishRefresh();
            dismiss();
        });
    }

    @Override
    protected void initView() {
        mBinding.rvProblem.setNestedScrollingEnabled(false);
        mBinding.rvProblem.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvProblem.addItemDecoration(new GridSpacingItemDecoration(10));
        problemAdapter = new ProblemAdapter(this,mData);
        mBinding.rvProblem.setAdapter(problemAdapter);
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });

        mBinding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshType = 0;
            page = 1;
            mViewModel.getAppProblem(limit,page);
        });
        mBinding.smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            refreshType = 1;
            if (page < pageSize) {
                page++;
                mViewModel.getAppProblem(limit, page);
            } else {
                mBinding.smartRefreshLayout.finishLoadMore();
                Toast.makeText(this, R.string.not_more_data, Toast.LENGTH_SHORT).show();
            }
        });
        mBinding.smartRefreshLayout.autoRefresh();
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }
    public static void startActivity(Context context){
        Intent intent = new Intent(context, CommonProblemActivity.class);
        context.startActivity(intent);
    }
}